// // 模拟用户数据存储
// let users = JSON.parse(localStorage.getItem('users') || '[]');
// let currentUser = JSON.parse(localStorage.getItem('currentUser') || 'null');

// 确保数据正确初始化
const initializeUsers = () => {
  const usersData = localStorage.getItem('users');
  if (!usersData) {
    localStorage.setItem('users', JSON.stringify([]));
  }
  return JSON.parse(localStorage.getItem('users') || '[]');
};

let users = initializeUsers();
let currentUser = JSON.parse(localStorage.getItem('currentUser') || 'null');

// 添加登录时间检查
const MAX_SESSION_HOURS = 24; // 24小时后自动退出

export const auth = {
  // // 登录
  // login(username, password) {
  //   const user = users.find(u => u.username === username && u.password === password);
  //   if (user) {
  //     currentUser = user;
  //     localStorage.setItem('currentUser', JSON.stringify(user));
  //     return true;
  //   }
  //   return false;

  isAuthenticated() {
    if (!currentUser) return false;
    
    // 检查会话是否过期
    const loginTime = new Date(currentUser.loginTime || currentUser.createdAt);
    const now = new Date();
    const hoursDiff = (now - loginTime) / (1000 * 60 * 60);
    
    if (hoursDiff > MAX_SESSION_HOURS) {
      this.logout();
      return false;
    }
    
    return true;
  },
  
  // 登录 - 修复对比逻辑
  login(username, password) {
    console.log('登录尝试:', username);
    console.log('现有用户:', users);
    
    // 确保用户数据是最新的
    users = JSON.parse(localStorage.getItem('users') || '[]');
    
    const user = users.find(u => 
      u.username === username && u.password === password
    );
    
    if (user) {
      currentUser = user;
      localStorage.setItem('currentUser', JSON.stringify(user));
      console.log('登录成功:', user);
      return true;
    }
    console.log('登录失败: 用户名或密码错误');
    return false;
  },

  // 注册
  register(username, password, email) {
    // 重新读取最新数据
    users = JSON.parse(localStorage.getItem('users') || '[]');
    
    console.log('注册尝试:', username);
    console.log('现有用户:', users);

    if (users.some(u => u.username === username)) {
      return { success: false, message: '用户名已存在' };
    }

    const newUser = {
      id: Date.now(),
      username: username.trim(),
      password: password.trim(),
      email: email.trim(),
      createdAt: new Date().toISOString()
    };

    users.push(newUser);
    localStorage.setItem('users', JSON.stringify(users));
    
    currentUser = newUser;
    localStorage.setItem('currentUser', JSON.stringify(newUser));
    console.log('注册成功：', newUser);
    return { success: true, message: '注册成功' };
  },

  // 注销
  logout() {
    currentUser = null;
    localStorage.removeItem('currentUser');
    console.log('用户已退出');
  },

  // 检查是否登录
  isAuthenticated() {
    return currentUser !== null;
  },

  // 获取当前用户
  getCurrentUser() {
    return currentUser;
  },

  // 添加调试方法
  debug() {
    return {
      users: users,
      currentUser: currentUser,
      localStorageUsers: JSON.parse(localStorage.getItem('users') || '[]'),
      localStorageCurrent: JSON.parse(localStorage.getItem('currentUser') || 'null')
    };
  }
};